package com.odais.DataManagement;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.odais.Reservation.Reservation;
import com.odais.Reservation.ReservationList;
import com.odais.UserManagement.UserManager;
import com.odais.json.JSON_IO_Interface;

public class GetReservationListOnSQLite extends AsyncTask<Void, String, Void> 
{
	// ===========================================================
	// Constants
	// ===========================================================


	// ===========================================================
	// Fields
	// ===========================================================
	private Context mContext;
	private ReservationList mReservationList;
	private ArrayList<DataListener> mListeners;
	private ReservationRepository reservationRepository;
	private String selectColumn;
	private Object selectArg;

	// ===========================================================
	// Constructors
	// ===========================================================

	public GetReservationListOnSQLite(Context context, ReservationList reservationList, ArrayList<DataListener> listener) {
		super();
		this.mContext = context;
		this.mReservationList = reservationList;
		this.mListeners = listener;
		this.selectColumn = "";
		this.selectArg = "";
		
	}
	
	public GetReservationListOnSQLite(Context context, ReservationList reservationList, ArrayList<DataListener> listener, String selectColumn, Object selectArg) {
		super();
		this.mContext = context;
		this.mReservationList = reservationList;
		this.mListeners = listener;
		this.selectColumn = selectColumn;
		this.selectArg = selectArg;
	}
		
	// ===========================================================
	// Getter & Setter
	// ===========================================================


	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
		
	@Override
	protected void onPreExecute()
    { 
    	mReservationList =  new ReservationList();
    	mReservationList.create();

    	reservationRepository = new ReservationRepository(mContext);
        reservationRepository.Open();
    }

	@Override
    protected Void doInBackground(Void... unused)
	{
		if(selectColumn.equals(DataBaseHelper.COLUMN_TIMESTAMP))
		{
			mReservationList = reservationRepository.GetByTimestamp((String)selectArg);
			mReservationList.setListType(DataManager.UPDATE_TYPE_CONFIRMED_DATED_LIST);
		}
		else if(selectColumn.equals(DataManager.UPDATE_TYPE_FULL_LIST))
		{
			mReservationList = reservationRepository.GetAll();
			mReservationList.setListType(DataManager.UPDATE_TYPE_FULL_LIST);
		}
		else if(selectColumn.equals(DataManager.UPDATE_TYPE_PENDING_LIST))
		{
			mReservationList = reservationRepository.GetPending();
			mReservationList.setListType(DataManager.UPDATE_TYPE_PENDING_LIST);
		}
		else if(selectColumn.equals(DataManager.UPDATE_TYPE_CONFIRMED_LIST))
		{
			mReservationList = reservationRepository.GetConfirmed();
			mReservationList.setListType(DataManager.UPDATE_TYPE_CONFIRMED_LIST);
		}
		else
		{
			mReservationList = reservationRepository.GetPending();
			mReservationList.setListType(DataManager.UPDATE_TYPE_PENDING_LIST);
		}
    	return null;
    }

    @Override
    protected void onProgressUpdate(String... item) 
    {
    	//mProgressBar.set
    }

    @Override
    protected void onPostExecute(Void unused)
    {		   

    	reservationRepository.Close();
    	
        for (DataListener listener  : mListeners) 
        {
            listener.onDataUpdate(mReservationList);
        }
    	
//		Toast
//		    .makeText(mContext, "Done!", Toast.LENGTH_SHORT)
//		    .show();
  
    }
    


}